<?php

include 'confidential_settings.php';
// This include contains 5 variables:
//  - db_name: database name
//  - table_name_x: table name
//  - username: username
//  - password: password
//  - localhost: host name

function get_corr_value_year($year){
	include 'gas_corrections.php';

	$ret_corr_value = 0;
	for($i=0;$i<count($corr_year);$i++){
		if(trim($corr_year[$i]) == trim($year)){
			$ret_corr_value = $ret_corr_value + $corr_value[$i];
		}
	}
	return $ret_corr_value;
}

$dbh=mysql_connect ($localhost, $username, $password) or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db_name) or die(mysql_error()); 

$result = mysql_query("SELECT YEAR(date_entered), SUM(val) FROM $table_name_gas GROUP BY YEAR(date_entered)");

mysql_close($dbh);


mysql_data_seek ($result , 0);

$max = 0;
$nb = 0;
while($row = mysql_fetch_array($result)){
	$tmp = $row['SUM(val)']/100;
	$corr_value_tmp = get_corr_value_year($row['YEAR(date_entered)']);
	$tmp = $tmp + $corr_value_tmp;
	if($tmp > $max){
		$max = $tmp;
	}
	$nb = $nb + 1;
}
$max = $max * 1.05;

mysql_data_seek ($result , 0);

/*
echo '<img src="http://chart.apis.google.com/chart?cht=bvs&chd=t:';
if($row = mysql_fetch_array($result)){
	$tmp = $row['SUM(val)']/100;
	$corr_value_tmp = get_corr_value_year($row['YEAR(date_entered)']);
	$tmp = ($tmp+$corr_value_tmp) *100/$max;
	echo $tmp;
}
*/

echo <<<END
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Jaar');
        data.addColumn('number', 'gas verbruik [m3]');
        data.addRows([ ['2009', 0],
END;

while($row = mysql_fetch_array($result)){
	$tmp = $row['SUM(val)']/100;
	$corr_value_tmp = get_corr_value_year($row['YEAR(date_entered)']);
	$tmp = ($tmp+$corr_value_tmp);
	echo "['";
	echo $row['YEAR(date_entered)'];
	echo "',";
	echo $tmp;
	echo "],";
}

echo <<<END
        ]);

        var options = {
          width: 600, height: 400,
          title: 'Gas verbruik [m3]',
          hAxis: {title: 'Year',  titleTextStyle: {color: 'red'}},
	  legend: 'none'
        };

        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
END;

echo '<div id="chart_div"></div>';

?>
